/*
 * @Author: ZhanXie
 * @Date: 2020-03-17 14:15:40
 * @LastEditors: ZhanXie
 * @LastEditTime: 2020-03-30 15:27:55
 * @Description: 
 * @FilePath: /projectStudy/js-design/demo/src/05-迭代器-1.js
 */
// Iterator
class Iterator {
  constructor(container) {
    this.list = container.list
    this.index = 0
  }

  next() {
    if (this.hasNext()) {
      return this.list[this.index++]
    }
    return null
  }

  hasNext() {
    if (this.index >= this.list.length) {
      return false
    }
    return true
  }
}

class Container {
  constructor(list) {
    this.list = list
  }

  getIterator() {
    return new Iterator(this)
  }
}

const container = new container([1, 2, 3, 4, 5])
const iterator = container.getIterator()
while (iterator.hasNext()) {
  console.log(iterator.next())
}